package jhsys.hithink.basepage.pages;

import java.util.List;

import jhsys.hithink.R;
import jhsys.hithink.basepage.HiApplication;
import jhsys.hithink.db.HiBackground;
import jhsys.hithink.db.HiDatabase;
import jhsys.hithink.utils.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class HiInfoBkgSets extends Activity implements OnItemSelectedListener,
		ViewFactory {

	private Gallery gallery;
	private ImageSwitcher imageSwitcher;
	private ImageAdapter imageAdapter;

	private List<HiBackground> mBGList;

	private static Handler mhande = null;

	public static void setMhande(Handler hande) {
		mhande = hande;
	}

	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;

		public ImageAdapter(Context context) {
			mContext = context;
			TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
			mGalleryItemBackground = typedArray.getResourceId(
					R.styleable.Gallery_android_galleryItemBackground, 0);
		}

		public int getCount() {
			return mBGList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(mContext);
			Log.e("HiInfoBkgSets", "posiyion:" +position);
			imageView.setImageBitmap(getBgBitmap(mBGList.get(position)));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(384, 225));
			imageView.setBackgroundResource(mGalleryItemBackground);
			return imageView;
		}
		
	}
	
	private Bitmap getBgBitmap(HiBackground bg) {
		Bitmap bitmap = Utils.getBitmap(bg.getName());
		return bitmap;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		
		Bitmap bitmap = getBgBitmap(mBGList.get(position));
		HiApplication.bgBitmap = new BitmapDrawable(bitmap);
		imageSwitcher.setImageResource(position);
		HiApplication.curBackpage = Integer.valueOf(mBGList.get(position).getID());
		mhande.sendEmptyMessage(HiApplication.curBackpage);
		recodeback(HiApplication.curBackpage);
		Log.e("HiInfoBg", "select id " + HiApplication.curBackpage);
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		ImageView imageView = new ImageView(this);
		imageView.setBackgroundColor(0xFF000000);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		return imageView;
	}

	private void recodeback(int curBackpage) {
		SharedPreferences sp=getSharedPreferences("localsets", 0);	
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt("curCurbackpage", curBackpage).commit();
		HiApplication.curBackpage=curBackpage;

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infobkg);

		gallery = (Gallery) findViewById(R.id.gallery);
		// get all BackGround
		final HiDatabase database = new HiDatabase(this);
		try {
			mBGList = database.getBackGround();
			Log.e("HiInfoBkgSets", "mBGList size :" + mBGList.size() + " id: "
					+ HiApplication.curBackpage);
			imageAdapter = new ImageAdapter(this);
			gallery.setAdapter(imageAdapter);
			gallery.setOnItemSelectedListener(this);
			
			for (int i = 0; i < mBGList.size(); i++) {
				if (HiApplication.curBackpage == Integer.valueOf(mBGList.get(i)
						.getID())) {
					gallery.setSelection(i);
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
		imageSwitcher.setFactory(this);

		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
	}
}
